__author__ = """waen <wangjian@21epub.com>"""
__docformat__ = 'restructuredtext'


from AccessControl import ClassSecurityInfo
from zope.interface import implements

from Products.ATContentTypes.content.base import registerATCT

from Products.Archetypes import atapi
from Products.ATContentTypes.content.folder import ATFolderSchema
from Products.ATContentTypes.configuration import zconf


from Products.ePub.config import PROJECTNAME
from Products.ePub.interfaces import IComixFolder
from Products.ePub.content.folder import ePubFolder
from Products.ePub import ePubMessageFactory as _

#  relatedItems 
from Products.CMFCore.permissions import ModifyPortalContent
from archetypes.referencebrowserwidget import ReferenceBrowserWidget

from Products.validation.config import validation
from Products.validation.validators.SupplValidators import MaxSizeValidator
from Products.validation import V_REQUIRED

validation.register(MaxSizeValidator('checkImageMaxSize',
                                     maxsize=zconf.ATImage.max_file_size))

# Schema definition
ComixSchema = ATFolderSchema.copy() + atapi.Schema((
     atapi.ReferenceField('relatedItems',
                          relationship = 'relatesTo',
                          multiValued = True,
                          isMetadata = True,
                          languageIndependent = False,
                          write_permission = ModifyPortalContent,
                          widget = ReferenceBrowserWidget(
                                                allow_search = True,
                                                allow_browse = True,
                                                show_indexes = False,
#                                                force_close_on_insert = True,
                                                label = _(u'label_related_items', default=u'Related Items'),
                                                description = '',
                                                visible = {'edit' : 'visible', 'view' : 'invisible' }
                                                )
                          ),
                                                                
    atapi.StringField('comixisbn',
	                required = True,
					searchable = True,
					widget = atapi.StringWidget(
					    label = _(u'label_info_comixisbn',default=u'Comix ISBN'),
						description = _(u'help_info_comixisbn',default=u'')
					)
	),
	atapi.DateTimeField('onsale',
	                required = False,
					searchable = True,
					widget = atapi.CalendarWidget(
					    label = _(u'label_info_onsale',default=u'On Sale'),
						description = _(u'help_info_onsale',default=u'Please input a on-sale date.')
					)
	),
	atapi.ImageField('cover',
	                required=True,
                    primary=True,
                    languageIndependent=True,
                    storage = atapi.AnnotationStorage(migrate=True),
                    swallowResizeExceptions = zconf.swallowImageResizeExceptions.enable,
                    pil_quality = zconf.pil_config.quality,
                    pil_resize_algo = zconf.pil_config.resize_algo,
                    max_size = zconf.ATImage.max_image_dimension,
                    sizes= {'large'   : (768, 768),
                            'preview' : (400, 400),
                            'mini'    : (200, 200),
                            'thumb'   : (128, 128),
                            'tile'    :  (64, 64),
                            'icon'    :  (32, 32),
                            'listing' :  (16, 16),
                    },
                    validators = (('isNonEmptyFile', V_REQUIRED),
                                  ('checkImageMaxSize', V_REQUIRED)),
					widget = atapi.ImageWidget(
					    label = _(u'label_info_cover',default = u'Cover'),
						description = _(u'help_info_cover',default=u'')
					)	               
	),
	atapi.StringField('type',
	                requied = False,
					vocabulary = '_getComixType',
					enforceVocabulary = 1,
					widget = atapi.SelectionWidget(
					    label = _(u'label_info_comixtype',default=u'Comix Type')
					)
	),
))

class ComixFolder(ePubFolder):
    """A chapter,which contains comix pages """
    portal_type      = 'ComixFolder'
    archetype_name   = 'ComixFolder'
    _atct_newTypeFor = {'portal_type' : 'CMF Folder', 'meta_type' : 'Comix Folder'}

    implements(IComixFolder)
    schema = ComixSchema
    def _getComixType(self):
        return atapi.DisplayList((
    	    ('1', _(u'type_info_funny',default=u'Funny')),
            ('2', _(u'type_info_horror',default=u'Horror')),
            ('3', _(u'type_info_love',default=u'Love'))
        ))
		
    def getChapters(self):
	    return self.getFolderContents()

registerATCT(ComixFolder,PROJECTNAME)